<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <title>Unicode 编码转换工具</title>
  <style>
    body {
      font-family: sans-serif;
      margin: 2em;
      background-color: #f9f9f9;
    }
    textarea {
      width: 100%;
      height: 200px;
      margin-bottom: 1em;
      font-family: monospace;
      font-size: 14px;
    }
    button {
      margin-right: 1em;
      padding: 0.5em 1em;
      font-size: 14px;
    }
  </style>
</head>
<body>
  <h2>Unicode 编码转换工具</h2>

  <textarea id="input" placeholder="输入中文或 \uXXXX 格式的字符串..."></textarea>

  <div>
    <button onclick="toUnicode()">转为 Unicode（\uXXXX）</button>
    <button onclick="fromUnicode()">转为 中文</button>
    <button onclick="clearText()">清空</button>
  </div>

  <textarea id="output" placeholder="转换结果..." readonly></textarea>

  <script>
    function toUnicode() {
      const input = document.getElementById("input").value;
      let result = '';
      for (let i = 0; i < input.length; i++) {
        const code = input.charCodeAt(i);
        if (code > 127) {
          result += '\\u' + code.toString(16).padStart(4, '0');
        } else {
          result += input[i];
        }
      }
      document.getElementById("output").value = result;
    }

    function fromUnicode() {
      const input = document.getElementById("input").value;
      const result = input.replace(/\\u([0-9a-fA-F]{4})/g, (match, group) => {
        return String.fromCharCode(parseInt(group, 16));
      });
      document.getElementById("output").value = result;
    }

    function clearText() {
      document.getElementById("input").value = '';
      document.getElementById("output").value = '';
    }
  </script>
</body>
</html>
